{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Replace Elements in an Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Medium"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: arrayChange"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #替换数组中的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>nums</code>&nbsp;，它包含 <code>n</code>&nbsp;个 <strong>互不相同</strong>&nbsp;的正整数。请你对这个数组执行 <code>m</code>&nbsp;个操作，在第 <code>i</code>&nbsp;个操作中，你需要将数字&nbsp;<code>operations[i][0]</code> 替换成&nbsp;<code>operations[i][1]</code>&nbsp;。</p>\n",
    "\n",
    "<p>题目保证在第 <code>i</code>&nbsp;个操作中：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>operations[i][0]</code>&nbsp;在&nbsp;<code>nums</code>&nbsp;中存在。</li>\n",
    "\t<li><code>operations[i][1]</code>&nbsp;在&nbsp;<code>nums</code>&nbsp;中不存在。</li>\n",
    "</ul>\n",
    "\n",
    "<p>请你返回执行完所有操作后的数组。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,2,4,6], operations = [[1,3],[4,7],[6,1]]\n",
    "<b>输出：</b>[3,2,7,1]\n",
    "<b>解释：</b>我们对 nums 执行以下操作：\n",
    "- 将数字 1 替换为 3 。nums 变为 [<em><strong>3</strong></em>,2,4,6] 。\n",
    "- 将数字 4 替换为 7 。nums 变为 [3,2,<em><strong>7</strong></em>,6] 。\n",
    "- 将数字 6 替换为 1 。nums 变为 [3,2,7,<em><strong>1</strong></em>] 。\n",
    "返回最终数组 [3,2,7,1] 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><b>输入：</b>nums = [1,2], operations = [[1,3],[2,1],[3,2]]\n",
    "<b>输出：</b>[2,1]\n",
    "<b>解释：</b>我们对 nums 执行以下操作：\n",
    "- 将数字 1 替换为 3 。nums 变为 [<em><strong>3</strong></em>,2] 。\n",
    "- 将数字 2 替换为 1 。nums 变为 [3,<em><strong>1</strong></em>] 。\n",
    "- 将数字 3 替换为 2 。nums 变为 [<em><strong>2</strong></em>,1] 。\n",
    "返回最终数组 [2,1] 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>n == nums.length</code></li>\n",
    "\t<li><code>m == operations.length</code></li>\n",
    "\t<li><code>1 &lt;= n, m &lt;= 10<sup>5</sup></code></li>\n",
    "\t<li><code>nums</code>&nbsp;中所有数字 <strong>互不相同</strong>&nbsp;。</li>\n",
    "\t<li><code>operations[i].length == 2</code></li>\n",
    "\t<li><code>1 &lt;= nums[i], operations[i][0], operations[i][1] &lt;= 10<sup>6</sup></code></li>\n",
    "\t<li>在执行第&nbsp;<code>i</code> 个操作时，<code>operations[i][0]</code>&nbsp;在&nbsp;<code>nums</code>&nbsp;中存在。</li>\n",
    "\t<li>在执行第&nbsp;<code>i</code>&nbsp;个操作时，<code>operations[i][1]</code>&nbsp;在&nbsp;<code>nums</code>&nbsp;中不存在。</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [replace-elements-in-an-array](https://leetcode.cn/problems/replace-elements-in-an-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [replace-elements-in-an-array](https://leetcode.cn/problems/replace-elements-in-an-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,4,6]\\n[[1,3],[4,7],[6,1]]', '[1,2]\\n[[1,3],[2,1],[3,2]]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        mp = {}\n",
    "        for x, y in reversed(operations):\n",
    "            mp[x] = mp.get(y, y)\n",
    "        return [mp.get(num, num) for num in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        res = {}\n",
    "        for i, j in reversed(operations):\n",
    "            res[i] = res.get(j, j)\n",
    "        nums = [res.get(i, i) for i in nums]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        mp = {}\n",
    "        for x, y in reversed(operations):\n",
    "            mp[x] = mp.get(y, y)\n",
    "        return [mp.get(num, num) for num in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        mp = {}\n",
    "        for x, y in reversed(operations):\n",
    "            mp[x] = mp.get(y, y)\n",
    "        return [mp.get(num, num) for num in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        #模拟会超时?\n",
    "        #对op预处理 1,3 3,6 6,5 就直接变成  1,5\n",
    "        dic = dict() #建立一个对应表 1:3  3:6  ->1:6\n",
    "        for op0, op1 in operations:\n",
    "            if op0 not in dic:\n",
    "                dic[op1] = op0\n",
    "            else:\n",
    "                dic[op1] = dic[op0]\n",
    "                #还要删除 dic里的 op1:op0\n",
    "                dic.pop(op0)\n",
    "        #dic反向:\n",
    "        dic2 = dict()\n",
    "        for d in dic:\n",
    "            dic2[dic[d]] = d\n",
    "        for i, x in enumerate(nums):\n",
    "            print(i,x)\n",
    "            if x in dic2:\n",
    "                nums[i] = dic2[x]\n",
    "        return nums\n",
    "\n",
    "            \n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        mp = {}\n",
    "        for x, y in reversed(operations):\n",
    "            mp[x] = mp.get(y, y)\n",
    "        return [mp.get(num, num) for num in nums]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], op: List[List[int]]) -> List[int]:\n",
    "        dicc=dict()\n",
    "        \n",
    "        for i in range(len(op)-1,-1,-1):\n",
    "            x,y=op[i]\n",
    "            dicc[x]=dicc[y] if y in dicc else y\n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "        \n",
    "        return [(dicc[i] if i in dicc else i) for i in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        M = {}\n",
    "        for a,b in operations[::-1]:\n",
    "            M[a] = M.get(b,b)\n",
    "        return [M.get(a,a) for a in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        M = {}\n",
    "        for a,b in operations[::-1]:\n",
    "            M[a] = M.get(b,b)\n",
    "        return [M.get(a,a) for a in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        m = {}\n",
    "        for i in range(len(operations) - 1, -1, -1):\n",
    "            m[operations[i][0]] = m.get(operations[i][1], operations[i][1])\n",
    "        return [m.get(num, num) for num in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        M = {}\n",
    "        for a,b in operations[::-1]:\n",
    "            M[a] = M.get(b,b)\n",
    "        return [M.get(a,a) for a in nums]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dic = {}\n",
    "        for x, y in operations[::-1]:\n",
    "            if y in dic:\n",
    "                dic[x] = dic[y]\n",
    "            else:\n",
    "                dic[x] = y \n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] in dic:\n",
    "                nums[i] = dic[nums[i]]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        M = {}\n",
    "        for a,b in operations[::-1]:\n",
    "            M[a] = M.get(b,b)\n",
    "        return [M.get(a,a) for a in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        M = {}\n",
    "        for a,b in operations[::-1]:\n",
    "            M[a] = M.get(b, b)\n",
    "        return [M.get(a, a) for a in nums]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        # 解法二：对替换数组进行倒序求解，然后一次替换。\n",
    "        d = dict()\n",
    "        for k, v in operations[::-1]:\n",
    "            # 这一步实现一步到位，可以一次变换到达最后一个\n",
    "            if v in d.keys():\n",
    "                d[k] = d[v]\n",
    "            else:\n",
    "                d[k] = v\n",
    "        print(d)\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] in d:\n",
    "                nums[i] = d[nums[i]]\n",
    "        return nums\n",
    "\n",
    "        #解法一：按模拟过程，通过转换成含index的map解\n",
    "        # res = [0] * len(nums)\n",
    "        # numsmap = {}\n",
    "        # for index, num in enumerate(nums):\n",
    "        #     numsmap[num] = index\n",
    "        # print(nums, operations, numsmap)\n",
    "        # for i, j in operations:\n",
    "        #     print(i, j)\n",
    "        #     index = numsmap[i]\n",
    "        #     numsmap.pop(i)\n",
    "        #     numsmap[j] = index\n",
    "        # print(numsmap)\n",
    "        # #将map还原成数组\n",
    "        # for key, val in numsmap.items():\n",
    "        #     print(key, val)\n",
    "        #     res[val] = key\n",
    "        # return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        mp = dict()\n",
    "        for x, y in operations[::-1]:\n",
    "            mp[x] = mp.get(y, y)\n",
    "        nums = [ mp.get(x, x) for x in nums]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        M={}\n",
    "        for a,b in operations[::-1]:\n",
    "            M[a]=M.get(b,b)\n",
    "        return [M.get(a,a) for a in nums]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        mp = dict()\n",
    "        for x, y in operations[::-1]:\n",
    "            mp[x] = mp.get(y, y)\n",
    "        nums = [ mp.get(x, x) for x in nums]\n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        tran = dict()\n",
    "        rever = dict()\n",
    "        for k, v in operations:\n",
    "            if k in rever:\n",
    "                tran[rever[k]] = v\n",
    "                rever[v] = rever[k]\n",
    "                del rever[k]\n",
    "            else:\n",
    "                tran[k] = v\n",
    "                rever[v] = k\n",
    "        \n",
    "        for i, n in enumerate(nums):\n",
    "            if n in tran:\n",
    "                nums[i] = tran[n]\n",
    "        \n",
    "        return nums\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        num2idx = {}\n",
    "        for i, num in enumerate(nums):\n",
    "            num2idx[num] = i\n",
    "        for before, after in operations:\n",
    "            num2idx[after] = num2idx.pop(before)\n",
    "        res = [0] * len(nums)\n",
    "        for key, value in zip(num2idx.keys(), num2idx.values()):\n",
    "            res[value] = key\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        que = dict()\n",
    "        for i, j in enumerate(nums):\n",
    "            que[j] = i \n",
    "        for a, b in operations:\n",
    "            idx = que[a]\n",
    "            del que[a]\n",
    "            que[b] = idx \n",
    "        ans = [0] * len(nums) \n",
    "        for k, v in que.items():\n",
    "            ans[v] = k \n",
    "        return ans "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        records = defaultdict(int)\n",
    "        for i, j in enumerate(nums):\n",
    "            records[j] = i\n",
    "        for m, n in operations:\n",
    "            records[n] = records[m]\n",
    "            records.pop(m)\n",
    "        ans = nums.copy()\n",
    "        for k, v in records.items():\n",
    "            ans[v] = k\n",
    "        \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dic = {}\n",
    "        for i, num in enumerate(nums):\n",
    "            dic[num] = i\n",
    "        for x, y in operations:\n",
    "            idx = dic[x]\n",
    "            dic[y] = idx\n",
    "            del dic[x]\n",
    "        \n",
    "        for k, v in dic.items():\n",
    "            nums[v] = k\n",
    "\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nn = len(operations)\n",
    "        dic = {}\n",
    "        for i in range(n):\n",
    "            dic[nums[i]]=i\n",
    "        for i in range(nn):\n",
    "            if operations[i][0] in dic:\n",
    "                nums[dic[operations[i][0]]]= operations[i][1]\n",
    "                dic[nums[dic[operations[i][0]]]] = dic[operations[i][0]]\n",
    "\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        nn = len(operations)\n",
    "        dic = {}\n",
    "        for i in range(n):\n",
    "            dic[nums[i]]=i\n",
    "        for i in range(nn):\n",
    "            if operations[i][0] in dic:\n",
    "                tem = operations[i][0]\n",
    "                nums[dic[tem]]= operations[i][1]\n",
    "                dic[nums[dic[tem]]] = dic[operations[i][0]]\n",
    "\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        myd={}\n",
    "        for i,m in enumerate(nums):\n",
    "            myd[m]=i\n",
    "        for a,b in operations:\n",
    "            pos=myd[a]\n",
    "            myd[b]=pos\n",
    "            myd[a]=-1\n",
    "        ans=[0 for j in nums]\n",
    "        for j in myd:\n",
    "            if myd[j] !=-1:\n",
    "                ans[myd[j]]=j\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import json\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        nums_dict = {value: index for index, value in enumerate(nums)}\n",
    "        for i in operations:\n",
    "            nums[nums_dict.get(i[0])] = i[1]\n",
    "            nums_dict[i[1]] = nums_dict.get(i[0])\n",
    "\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        \n",
    "        mp = {n: i for i, n in enumerate(nums)}\n",
    "\n",
    "\n",
    "\n",
    "        for a, b in operations:\n",
    "            idx = mp[a]\n",
    "            nums[idx] = b\n",
    "            del mp[a]\n",
    "            mp[b] = idx\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        n = len(nums)\n",
    "        map = dict()\n",
    "        for i in range(n):\n",
    "            map[nums[i]] = i\n",
    "        for x, y in operations:\n",
    "            old = map[x]\n",
    "            nums[old] = y\n",
    "            map[y] = old\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        nums_dict = {v : i for i, v in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            idx = nums_dict[x]\n",
    "            nums[idx] = y \n",
    "            del nums_dict[x]\n",
    "            nums_dict[y] = idx\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        D = {}\n",
    "        for idx,i in enumerate(nums):\n",
    "            D[i] = idx\n",
    "        for pre,new in operations:\n",
    "            nums[D[pre]] = new\n",
    "            D[new] = D[pre]\n",
    "            del D[pre]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        index_dict = {nums[i]: i for i in range(len(nums))}\n",
    "        for i in range(len(operations)):\n",
    "            index = index_dict[operations[i][0]]\n",
    "            nums[index] = operations[i][1]\n",
    "            index_dict.pop(operations[i][0])\n",
    "            index_dict[operations[i][1]] = index\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        idx = {num: i for i, num in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            i = idx[x]\n",
    "            nums[i] = y\n",
    "            del idx[x]\n",
    "            idx[y] = i\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        m = {}\n",
    "\n",
    "        for k in range(len(nums)):\n",
    "            m[nums[k]] = k\n",
    "\n",
    "        for operation in operations:\n",
    "            f, t = operation\n",
    "\n",
    "            m[t] = m[f]\n",
    "            m[f] = -1\n",
    "\n",
    "        for k in m:\n",
    "            if m[k] == -1:\n",
    "                continue\n",
    "            nums[m[k]] = k\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        idx = {num: i for i, num in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            i = idx[x]\n",
    "            nums[i] = y\n",
    "            del idx[x]\n",
    "            idx[y] = i\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        p={}\n",
    "        for i in range(len(nums)):\n",
    "            p[nums[i]]=i\n",
    "        for operation in operations:\n",
    "            nums[p[operation[0]]]=operation[1]\n",
    "            p[operation[1]]=p[operation[0]]\n",
    "            p.pop(operation[0])\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dic = {}\n",
    "        for i, v in enumerate(nums):\n",
    "            dic[v] = i\n",
    "        for old, new in operations:\n",
    "            index = dic.pop(old)\n",
    "            nums[index] = new \n",
    "            dic[new] = index\n",
    "        return nums\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "\n",
    "\n",
    "\n",
    "        exists = {}\n",
    "\n",
    "\n",
    "        for i, num in enumerate(nums):\n",
    "\n",
    "            exists[num] = i\n",
    "\n",
    "        \n",
    "        for a, b in operations:\n",
    "\n",
    "            t = exists[a]\n",
    "\n",
    "            nums[t] = b\n",
    "            exists[b] = t\n",
    "            del exists[a]\n",
    "\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dct = dict()\n",
    "        for i in range(len(nums)):\n",
    "            dct[nums[i]] = i\n",
    "        for op in operations:\n",
    "            nums[dct[op[0]]] = op[1]\n",
    "            dct[op[1]] = dct[op[0]]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        tag = {}\n",
    "        for k, i in enumerate(nums):\n",
    "            tag[i] = k\n",
    "        for i, j in operations:\n",
    "            nums[tag[i]] = j\n",
    "            tag[j] = tag[i]\n",
    "            del tag[i]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dic=collections.defaultdict(int)\n",
    "        for i,v in enumerate(nums):\n",
    "            dic[v]=i\n",
    "        \n",
    "        for s,e in operations:\n",
    "            \n",
    "            dic[s],dic[e]=-1,dic[s]\n",
    "\n",
    "        for x in dic:\n",
    "            if dic[x]!=-1:\n",
    "                nums[dic[x]]=x\n",
    "\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dicts = {e:i for i,e in enumerate(nums)}\n",
    "        for i,j in operations:\n",
    "            inds = dicts[i]\n",
    "            nums[inds] = j\n",
    "            dicts[j] = inds\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        idx = {num: i for i, num in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            i = idx[x]\n",
    "            nums[i] = y\n",
    "            del idx[x]\n",
    "            idx[y] = i\n",
    "        return nums\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dicts = {e:i for i,e in enumerate(nums)}\n",
    "        for i,j in operations:\n",
    "            inds = dicts[i]\n",
    "            nums[inds] = j\n",
    "            del dicts[i]\n",
    "            dicts[j] = inds\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        counter=dict()\n",
    "        for i in range(len(nums)):\n",
    "            counter[nums[i]]=i\n",
    "        m=len(operations)\n",
    "        for i in range(m):\n",
    "            nums[counter[operations[i][0]]]=operations[i][1]\n",
    "            counter[operations[i][1]]=counter[operations[i][0]]\n",
    "        return nums "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dic = {}\n",
    "        for i, v in enumerate(nums):\n",
    "            dic[v] = i\n",
    "        for old, new in operations:\n",
    "            index = dic.pop(old)\n",
    "            nums[index] = new \n",
    "            dic[new] = index\n",
    "        return nums\n",
    "\n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        dict = {n: i for i, n in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            i = dict[x]\n",
    "            nums[i] = y\n",
    "            del dict[x]\n",
    "            dict[y] = i\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        num_index_map = {num: i for i, num in enumerate(nums)}\n",
    "\n",
    "        for operation in operations:\n",
    "            target = operation[0]\n",
    "            replacement = operation[1]\n",
    "            index = num_index_map[target]\n",
    "            nums[index] = replacement\n",
    "            # 更新字典中的索引值\n",
    "            num_index_map[replacement] = index\n",
    "            del num_index_map[target]\n",
    "    \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        idx = {num: i for i, num in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            i = idx[x]\n",
    "            nums[i] = y\n",
    "            del idx[x]\n",
    "            idx[y] = i\n",
    "        return nums\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        idx = { val:i for i, val in enumerate(nums)}\n",
    "        for x, y in operations:\n",
    "            i = idx[x]\n",
    "            nums[i] = y\n",
    "            del idx[x]\n",
    "            idx[y] = i \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        # 关键在于怎么找到operations[i][0]并替换成operations[i][1]，所以维护一个字典表，存储元素及其对应的位置\n",
    "        nums_ind = {}\n",
    "        for i, num in enumerate(nums):\n",
    "            nums_ind[num] = i\n",
    "        for operation in operations:\n",
    "            # 获取operation[0]所在的位置\n",
    "            i = nums_ind[operation[0]]\n",
    "            # 替换nums\n",
    "            nums[i] = operation[1]\n",
    "            # 维护operation[1]在字典中\n",
    "            nums_ind[operation[1]] = i\n",
    "            # 移除operation[0]\n",
    "            del nums_ind[operation[0]]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        index_dict = {nums[i]: i for i in range(len(nums))}\n",
    "        for i in range(len(operations)):\n",
    "            index = index_dict[operations[i][0]]\n",
    "            nums[index] = operations[i][1]\n",
    "            index_dict.pop(operations[i][0])\n",
    "            index_dict[operations[i][1]] = index\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        nums_hash = {v:i for i, v in enumerate(nums)}\n",
    "        for item in operations:\n",
    "            idx = nums_hash[item[0]]\n",
    "            nums[idx] = item[1]\n",
    "            del nums_hash[item[0]]\n",
    "            nums_hash[item[1]] = idx\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        num2idx = {}\n",
    "        for idx, ii in enumerate(nums):\n",
    "            num2idx[ii] = idx\n",
    "        \n",
    "        for ii in operations:\n",
    "            u = ii[0]\n",
    "            v = ii[1]\n",
    "            idx = num2idx[u]\n",
    "            nums[idx] = v\n",
    "            del num2idx[u]\n",
    "            num2idx[v] = idx\n",
    "        \n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\n",
    "        pos = dict()\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            pos[nums[i]] = i\n",
    "        for a, b in operations:\n",
    "            nums[pos[a]] = b\n",
    "            pos[b] = pos[a]\n",
    "        return nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def arrayChange(self, nums: List[int], operations: List[List[int]]) -> List[int]:\r\n",
    "        idx = {}\r\n",
    "        for i, num in enumerate(nums):\r\n",
    "            idx[num] = i\r\n",
    "        for i, j in operations:\r\n",
    "            nums[idx[i]], idx[j] = j, idx[i]\r\n",
    "        return nums\r\n",
    "    "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
